package com.rsscollector.controller.taglibs.app;

import org.apache.log4j.Logger;

import javax.servlet.jsp.ErrorData;
import javax.servlet.jsp.JspException;
import java.io.IOException;

public class LogErrorTag extends ELTagSupport {

    private static Logger logger = Logger.getLogger(LogErrorTag.class.getName());

    @Override
    public void doTag() throws JspException, IOException {
        ErrorData errorData = null;
        try {
            errorData = (ErrorData) evaluate("${pageContext.errorData}", ErrorData.class);
        } catch(Exception e) {
            // Failed to evaluate expression
        }
        if (errorData != null) {
            String requestUri = errorData.getRequestURI();
            int statusCode = errorData.getStatusCode();
            Throwable throwable = errorData.getThrowable();
            String errorMessage = "An error happens for request: '" + requestUri + "', status code:" + statusCode;
            logger.error(errorMessage, throwable);
        }
    }
}
